import TimeShow from '../../utils/TimeShow'
import getUserInfo from '../../utils/getUserInfo'
import deleteWei from '../../utils/deleteWei'
Page({
  data: {
    zan: []
  },

  onLoad: function (options) {
    this.getGift()
  },

  onPullDownRefresh: function () {

  },

  // 获得送礼消息
  async getGift() {
    let sonli = wx.getStorageSync('sonli')
    if(sonli.length <= 0) return
    if (sonli) {
      let ids = []
      sonli.forEach((item) => {
        if(!ids.includes(item.userId)) {
          ids.push(item.userId)
        }
      })
      const users = await getUserInfo(ids)
      let hids = []
      sonli.forEach(item => {
        if(item.isweidu) {
          getApp().globalData.wdxx -= 1
          if(!hids.includes(item.userId) && item['isku']) hids.push(item.userId)
        }
          if(!item['headpic'] && !item['username']) {
            users.forEach(ele => {
              if(ele.id == item.userId) {
                item['headpic'] = ele.headpic
                item['username'] = ele.username
              }
            })
          }
          item['time'] = TimeShow(item['stime'])
          item.isweidu = false
      })
      if(hids.length > 0) deleteWei(hids)
      sonli.reverse()
      wx.setStorageSync('sonli', sonli)
      this.setData({
        zan: sonli
      })
    }
  },

  // 跳转到用户详情页
  hnadUser(e) {
    console.log(e)
    const { chatid, username } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/demes/demes?chatId=${chatid}&username=${username}`
    })
  }

})